import { createRouter, createWebHistory } from 'vue-router'
import HomePage from "@/views/HomePage.vue";
import Login from "@/views/Login.vue";
import AllBlog from "@/views/AllBlog.vue";
import ArticleDetail from "@/views/ArticleDetail.vue";
import MyInfo from "@/views/MyInfo.vue";
import AddArticle from "@/views/AddArticle.vue";
import Admin from "@/views/Admin.vue";
import UserManage from "@/views/UserManage.vue";
import pwdReset from "@/views/PwdReset.vue";
import ArticleManage from "@/views/ArticleManage.vue";
import ApplicationManage from "@/views/ApplicationManage.vue";
import CategoryManage from "@/views/CategoryManage.vue";
import DeepSeek from "@/views/DeepSeek.vue";




const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'homepage',
      component: HomePage
    },
    {
      path: '/deepseek',
      name: 'deepseek',
      component: DeepSeek
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/allBlog',
      name: 'allBlog',
      component: AllBlog
    },
    {
      path: '/articleDetail/:id',
      name: 'ArticleDetail',
      component: ArticleDetail
    },
    {
      path: '/myInfo',
      name: 'MyInfo',
      component: MyInfo
    },
    {
      path: '/application',
      name: 'application',
      component: Application
    },
    {
      path: '/addArticle',
      name: 'AddArticle',
      component: AddArticle
    },
    {
      path: '/admin',
      name: 'Admin',
      component: Admin,
      children: [
        {
          path: 'userManage',
          name: 'UserManage',
          component: UserManage
        },
        {
          path: 'refresh-model',
          name: 'RefreshModel',
          component: RefreshModel
        },
        {
          path: 'homeAdmin',
          name: 'homeAdmin',
          component: HomeAdmin
        },
        {
          path: 'categoryManage',
          name: 'CategoryManage',
          component: CategoryManage
        },
        {
          path: 'pwdReset',
          name: 'pwdReset',
          component: pwdReset
        },
        {
          path: 'articleManage',
          name: 'articleManage',
          component: ArticleManage
        },
        {
          path: 'applicationManage',
          name: 'applicationManage',
          component: ApplicationManage
        },
        {
          path: 'commentManage',
          name: 'commentManage',
          component: CommentManage
        },
      ]
    },



/*    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    }*/
  ]
})

// 导入element提示语的组件
import {
  ElMessage
} from 'element-plus'
import HomeAdmin from "@/views/HomeAdmin.vue";
import CommentManage from "@/views/CommentManage.vue";
import Application from "@/views/Application.vue";
import RefreshModel from "@/views/RefreshModel.vue";


// 添加请求拦截器
// 拦截器的第二部分，第一部分在main.js里面
/*router.beforeEach((to, from, next) => {

  // 获取存储localStorage的token
  let token = window.localStorage.getItem('token')
  // 获取存储token的开始时间
  const tokenStartTime = window.localStorage.getItem('tokenStartTime')
  // 后台给出的token有效时间，一个星期 单位 是秒
  //定义天6过期，让用户重新登录一下， 用户总不可能在一个页面挂机一天吧
  const timeOver =  6 * 24 * 3600 * 1000
  // 当前时间
  let date = new Date().getTime()
  // 如果大于说明是token过期了
  if(date - tokenStartTime > timeOver) {
    token = null
  }
  // 如果token过期了
  if (!token) {
    if (to.path == '/login') return next()
    // 注意要import element的Message组件
    ElMessage.error("登录状态过期，请重新登录")
    return next('/login')
    // 如果token没有过期，又是选择了登录页面就直接重定向到首页，不需要重新输入账户密码
  } else if (to.path == '/login') {
    return next('/')
  }
  next()

})*/


export default router
